package com.pingan.internal.point.shopping.service.impl;

import com.pingan.internal.point.shopping.common.utils.GiftIdGenerate;
import com.pingan.internal.point.shopping.controller.form.GoodsForm;
import com.pingan.internal.point.shopping.controller.form.GoodsSaveForm;
import com.pingan.internal.point.shopping.controller.response.DataPageList;
import com.pingan.internal.point.shopping.entity.Goods;
import com.pingan.internal.point.shopping.repository.GoodsRepository;
import com.pingan.internal.point.shopping.service.GoodsService;
import com.pingan.internal.point.shopping.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Value("${point.goods.upload.path}")
    private String path;

    @Resource
    private GoodsRepository goodsRepository;



    @Override
    public DataPageList<GoodsVo> list(GoodsForm form) {
        List<Goods> goods = goodsRepository.findAll();
        DataPageList<GoodsVo> dpl = new DataPageList<>();
        List<GoodsVo> vos = new ArrayList<>();
        goods.forEach(good->{
            GoodsVo vo =new GoodsVo();
            vo.build(good);
            vos.add(vo);
        });
        dpl.putList(vos);
        return dpl;
    }

    @Override
    public void save(GoodsSaveForm form) {
        Goods goods = new Goods();
        goods.setOperator(form.getOperator());

        //商品名称
        goods.setGoodsName(form.getGoodsName());
        //商品价格
        goods.setGoodsPoints(form.getGoodsPoints());
        //商品数量
        goods.setGoodsQuantity(form.getGoodsQuantity());
        //商品标签
        goods.setGoodsTag(form.getGoodsTag());
        //商品图片附件
        //private Long igGoodsImgId;
        //是否为限制类型
        goods.setLimitType(form.isLimitType());
        //每个会员限制数量
        goods.setLimitCount(form.getLimitCount());

        //@OneToOne
        //private Accessory igGoodsImg;
        goods.setGoodsImageName(form.getGoodsImageName());

        //开始时间
        goods.setBeginTime(form.getBeginTime());
        //结束时间
        goods.setEndTime(form.getEndTime());
        //是否展示
        goods.setShowFlag(true);
        //内容
        goods.setContent(form.getContent());
        GiftIdGenerate gift = new GiftIdGenerate();
        goods.setGoodsSn(gift.toHexString());
        goodsRepository.save(goods);
    }

    @Override
    public void update(GoodsSaveForm form) {
        Goods goods = goodsRepository.findByGoodsSn(form.getGoodsSn());
        //商品名称
        goods.setGoodsName(form.getGoodsName());
        //商品价格
        goods.setGoodsPoints(form.getGoodsPoints());
        //商品数量
        goods.setGoodsQuantity(form.getGoodsQuantity());
        goodsRepository.save(goods);
    }
}
